import { http } from '@kit.NetworkKit'
import { ApLog } from '../util/HiAnspLog'

const TAG: string = "asnpNet"

export namespace asnpNet {

  export class AsnpCommonHeader {
    cookie = "cookie one"
    appId = "my id"
  }

  interface NetCallBack<T> {
    onSuccess(data: T): void

    onFail(code: number, errMsg: string): void
  }

  export class NetError {
    code: number = -1
    errMsg: string = "not set"

    constructor(code: number, msg?: string) {
      this.code = code
      this.errMsg = msg ?? "unknown"
    }
  }

  export function newClient(): AsnpSimpleHttp {
    return new AsnpSimpleHttp()
  }

  export class AsnpSimpleHttp {
    private url: string
    private extraData: Object
    private options: http.HttpRequestOptions

    constructor() {
      this.url = ''
      this.options = {
        method: http.RequestMethod.GET,
        extraData: this.extraData,
        header: {
          // ...(new asnpNet.AsnpCommonHeader()),
          'Content-Type': 'application/json'
        },
        readTimeout: 15000,
        connectTimeout: 15000
      }
    }


    setUrl(url: string): AsnpSimpleHttp {
      this.url = url
      return this
    }

    // setMethod(method: string): AsnpSimpleHttp {
    //   method = method.toUpperCase()
    //   switch (method) {
    //     case 'GET':
    //       this.options.method = http.RequestMethod.GET;
    //       break
    //     case 'HEAD':
    //       this.options.method = http.RequestMethod.HEAD;
    //       break
    //     case 'OPTIONS':
    //       this.options.method = http.RequestMethod.OPTIONS;
    //       break
    //     case 'TRACE':
    //       this.options.method = http.RequestMethod.TRACE;
    //       break
    //     case 'DELETE':
    //       this.options.method = http.RequestMethod.DELETE;
    //       break
    //     case 'POST':
    //       this.options.method = http.RequestMethod.POST;
    //       break
    //     case 'PUT':
    //       this.options.method = http.RequestMethod.PUT;
    //       break
    //     case 'CONNECT':
    //       this.options.method = http.RequestMethod.CONNECT;
    //       break
    //     default:
    //       this.options.method = http.RequestMethod.GET;
    //       break
    //   }
    //   return this
    // }

    addHeader(newHeader: Object): AsnpSimpleHttp {
      let oldHeader = this.options.header
      this.options.header = {
        ...newHeader,
        ...oldHeader
      }
      return this
    }

    setExtraData(extraData: Object): AsnpSimpleHttp {
      this.options.extraData = extraData
      return this
    }

    async getToGetJson() {
      this.options.method = http.RequestMethod.GET
      let httpRequest = http.createHttp()
      try {
        let res = await httpRequest.request(this.url, this.options)
        if (res && res.resultType == http.HttpDataType.STRING) {
          httpRequest.destroy()
          return Promise.resolve(res.result)
        } else {
          httpRequest.destroy()
          return Promise.reject(new asnpNet.NetError(-1, "not json response"))
        }
      } catch (error) {
        httpRequest.destroy()
        ApLog.i(error.message, TAG)
        return Promise.reject(new asnpNet.NetError(error.code, error.message))
      }
    }

    async postToGetJson() {
      this.options.method = http.RequestMethod.POST
      let httpRequest = http.createHttp()
      try {
        let res = await httpRequest.request(this.url, this.options)
        if (res && res.resultType == http.HttpDataType.STRING) {
          httpRequest.destroy()
          return Promise.resolve(res.result)
        } else {
          httpRequest.destroy()
          return Promise.reject(new asnpNet.NetError(-1, "not json response"))
        }
      } catch (error) {
        httpRequest.destroy()
        ApLog.i(error.message, TAG)
        return Promise.reject(new asnpNet.NetError(error.code, error.message))
      }
    }
  }

}

